//
// Created by lymk on 2022/3/23.
//

#include "LambertShader.h"

VertexOut LambertShader::VertexShader(VertexIn vertexIn) {
    VertexOut vertexOut;
    vertexOut.color = vertexIn.color;
    vertexOut.worldPos = uniform->viewPort * uniform->model * vertexIn.pos;
    vertexOut.pos = uniform->perspective * vertexOut.worldPos;
    vertexOut.texCoord = vertexIn.texCoord;
    glm::vec4 tmp = glm::vec4(vertexIn.normal,0.f);
    tmp = uniform->viewPort * uniform->model * tmp;
    //tmp = tmp/tmp.w;
    vertexOut.normal = tmp;
    return vertexOut;
}

glm::vec4 LambertShader::FragmentShader(VertexOut vertexOut) {
    glm::vec3 finalColor = {0.f,0.f,0.f};
    glm::vec3 originColor = vertexOut.color;
    float envLight = 0.1;
    float diffuse = 1;
    float pointL = 0.2;
    for(int i=0;i<uniform->lights.size();i++){
        glm::vec3 dir = glm::vec4(uniform->lights[i].Position,1.f) - vertexOut.worldPos;
        dir = glm::normalize(dir);
        float tmp = 0.5f*(pointL * uniform->lights[i].intensity * glm::dot(vertexOut.normal,dir))+0.5;
        float I = envLight * diffuse + tmp;
        finalColor += I*originColor;
    }
    return {finalColor,1.f};
}

LambertShader::LambertShader() {
    uniform = new Uniform();
}
